<template>
  <div>
    <a-upload
        name="file"
        :fileList="fileList"
        :showUploadList="false"
        :beforeUpload="beforeUpload"
        @change="handleChangeFileList"
    >
      <a-button type="primary"><a-icon type="upload" />导入</a-button>
    </a-upload>
  </div>
</template>
<script>
import axios from "axios";

export default {
  props: {
    callback: { type: [Function] },
  },
  data() {
    return {
      fileList: [],
    }
  },
  methods: {
    handleChangeFileList(info) {
      let formData = new FormData()
      let fileList = info.fileList || []
      formData.append('file', fileList[0].originFileObj);
      axios({
        url: '/backend/path/import',
        method: 'post',
        header: {'Content-Type': 'multipart/form-data'},
        data: formData,
      }).then(() => {
        if(this.callback) {
          this.callback();
        }
        this.$success({
          title: this.$t('success_message'),
          content: this.$t('submit_success'),
          okText: this.$t('modal_ok'),
        });
      })
    },
    beforeUpload(file) {
      //避免走action,缓存file
      if (file.size / 1024 / 1024 > 10 ) {
        this.$message.error('文件过大（超过10MB），无法上传！');
        return false
      }
      return false
    },
  }
}
</script>
